/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Acceso.Security;

/**
 *
 * @author Hector Gutierrez
 */
public class consultarPuestoPersonalAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String[] ALLOWED = {"Administrador","Gestionar Notificaciones de Riesgo","Gestionar Analisis de Seguridad","Gestionar Notificaciones de Riesgo","Generar Informes de Riesgo"};

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String[] cedula = request.getParameterValues("ci");
        response.setContentType("text/json");
        JSONObject data = new JSONObject();

        data.put("puestoTrabajo", consultarPuesto(cedula[0]));
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }

    public JSONArray consultarPuesto(String cedula) {
        JSONArray puestos = new JSONArray();

        try {
            puestos = Modelo.DAL.PuestoTrabajo_DAO.consultarPuestoPersonal(cedula);
            return puestos;
        } catch (Exception e) {
            return null;
        }
    }
}
